package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.minewar.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMineRefugeDaoImpl extends TempDataObjectDao<UserMineRefuge> {

    public Map<Integer, Map<Integer, UserMineRefuge>> getUserMineRefugeMap(long userId) {
        Map<Integer, Map<Integer, UserMineRefuge>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_mine_refuge where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMineRefuge data = parse(rs);
                if(!dataMap.containsKey(data.getMineType())){
                    dataMap.put(data.getMineType(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getMineType()).put(data.getPitId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserMineRefuge data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_mine_refuge(mineType,pitId,userId,patronsIdList,joinTime,lastGetRewardTime) VALUES (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getMineType());
            call.setInt(i++, data.getPitId());
            call.setLong(i++, data.getUserId());
            call.setString(i++, StringUtils.listToString(data.getPatronsIdList(),','));
            call.setLong(i++, data.getJoinTime());
            call.setLong(i++, data.getLastGetRewardTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserMineRefuge data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_mine_refuge SET patronsIdList=?,joinTime=?,lastGetRewardTime=? where mineType=? and pitId=? and userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setString(i++, StringUtils.listToString(data.getPatronsIdList(),','));
            call.setLong(i++, data.getJoinTime());
            call.setLong(i++, data.getLastGetRewardTime());
            call.setInt(i++, data.getMineType());
            call.setInt(i++, data.getPitId());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserMineRefuge parseData(ResultSet rs) throws SQLException {
        UserMineRefuge data = new UserMineRefuge();
        data.setMineType(rs.getInt("mineType"));
        data.setPitId(rs.getInt("pitId"));
        data.setUserId(rs.getLong("userId"));
        data.setJoinTime(rs.getLong("joinTime"));
        data.setLastGetRewardTime(rs.getLong("lastGetRewardTime"));
        data.setPatronsIdList(StringUtils.stringToIntegerListNoZero(rs.getString("patronsIdList"), ","));
        return data;
    }
}
